﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EntityMessageDispatcher : Singleton<EntityMessageDispatcher>
{
    public const int c_iBattleMgrId = 4000;

    List<MessageBody> m_lisMessage = new List<MessageBody>();
    List<MessageBody> m_lisDel = new List<MessageBody>();

    public void SendMessage(string msgId, int senderId, int receiverId)
    {
        SendMessage(msgId, senderId, receiverId, 0, null);
    }

    public void SendMessage(string msgId, int senderId, int receiverId,float delay,params object []args)
    {
        MessageBody message = new MessageBody();
        message.msgId = msgId;
        message.senderId = senderId;
        message.receiveId = receiverId;
        message.delay = delay;
        message.args = args;
        message.passTime = 0;

        if(message.passTime >= message.delay)
        {
            if (receiverId == c_iBattleMgrId)
            {
                if(HeroBattleMgr.Instance != null)
                {
                    HeroBattleMgr.Instance.HandleMessage(message);
                }
            }
            else
            {
                BattleEntity reciver = BattleEntityFactory.Instance.GetBtEntity(message.receiveId);
                reciver.HandleMessage(message);
            }
        }
        else
        {
            m_lisMessage.Add(message);
        }
    }

    public void SendEntity2Node(string msgId,int senderId,params object[] args)
    {
        SendMessage(msgId, senderId, c_iBattleMgrId, 0, args);
    }
    public void SendNode2Entity(string msgId,int receiverId,params object[] args)
    {
        SendMessage(msgId, c_iBattleMgrId, receiverId, 0, args);
    }

    public void OnUpdate(float deltaTime)
    {
        for (int i = 0; i < m_lisMessage.Count; i++)
        {
            if (m_lisMessage[i].passTime >= m_lisMessage[i].delay)
            {
                if(m_lisMessage[i].receiveId != c_iBattleMgrId)
                {
                    BattleEntity reciver = BattleEntityFactory.Instance.GetBtEntity(m_lisMessage[i].receiveId);
                    reciver.HandleMessage(m_lisMessage[i]);
                }
                else
                {
                    if (HeroBattleMgr.Instance != null)
                    {
                        HeroBattleMgr.Instance.HandleMessage(m_lisMessage[i]);
                    }
                }
                m_lisDel.Add(m_lisMessage[i]);
            }
            else
            {
                m_lisMessage[i].passTime += deltaTime;
            }
        }
        if (m_lisDel.Count > 0)
        {
            for (int i = 0; i < m_lisDel.Count; i++)
            {
                m_lisMessage.Remove(m_lisDel[i]);
            }
            m_lisDel.Clear();
        }
    }


}

public class MessageBody
{
    public string msgId;
    public int senderId;
    public int receiveId;
    public float delay;
    public object [] args;
    public float passTime = 0;
}



public class MessageId
{
    //移动事件
    public const string Arrive = "arrive";
    //状态机事件
    public const string AttackFinish = "attack_finish";
    public const string BeHit = "be_hit";
    public const string ProtectBeHit = "protect_behit";
    public const string BulletArrive = "bullet_arrive";
}